#include <iostream>
#include <string>
#include <vector>
using namespace std;

//重新排列的含义
class Solution
{
public:
    long long validSubstringCount(string word1, string word2)
    {
        vector<int> v1(26), v2(26);
        for (auto i : word2)
        {
            v2[i - 'a']++;
        }

        int lastCharIndex = 0;
        for (int i = 0; i < 26; i++)
            if (v2[i] > 0)
                lastCharIndex = i;

        auto check = [&]() -> int
        {
            for (int i = 0; i <= lastCharIndex; i++)
                if (v1[i] < v2[i])
                    return +1;
                else if (v1[i] > v2[i])
                    return -1;
            return 0;
        };

        int start = 0;
        int end = 0;
        int ans = 0;
        while (end < (word1.size() - 1))
        {
            int op = check();
            if (op == 0)
            {
                ans++;
                end++;
                v1[word1[end] - 'a']++;
            }
            else if (op == -1)
            {
                v1[word1[start] - 'a']--;
                start++;
            }
            else
            {
                end++;
                v1[word1[end] - 'a']++;
            }
        }
        if (check() == 0)
            ans++;
        return ans;
    }
};

int main()
{
    cout << "Code3298" << endl;
    Solution s;
    cout << s.validSubstringCount("bcca", "abc");
    return 0;
}